package com.stupro.cjlu.service;

import com.stupro.cjlu.common.vo.ResultVO;
import com.stupro.cjlu.dao.CourseDAO;
import com.stupro.cjlu.dao.StudentCourseDAO;
import com.stupro.cjlu.entity.Course;
import com.stupro.cjlu.manager.CourseManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2021-05-10
 */
@Service
public class CourseService extends BaseService{

    @Autowired
    private CourseDAO courseDAO;

    @Autowired
    private StudentCourseDAO studentCourseDAO;

    @Autowired
    private CourseManager manager;

    @Transactional
    public ResultVO delCourseByCourseId(int id){
        studentCourseDAO.deleteByCourseId(id);
        return result(courseDAO.delById(id));
    }

    public ResultVO addCourse(Course course){
        return result(courseDAO.addCourse(course));
    }

    public ResultVO create(Course entity) {
        if (manager.get(entity.getCoID()) != null) {
            return failedResult("课程Id: " + entity.getCoID() + "已存在!");
        }
        if (manager.getTeacherById(entity.getTeaID()) == null) {
            return failedResult("授课教师Id: " + entity.getTeaID() + "不存在!");
        }

        manager.create(entity);
        return result("添加成功");
    }

    public ResultVO getPageCount(Integer teacherId, String name) {
        return result(manager.getPageCount(teacherId, name));
    }

}
